package com.sfeir.poc.portal.gui.client.util;

import java.util.ArrayList;
import java.util.List;

/**
 * This class aims to parse RSS feed content 
 * Rss from spreadsheets are organized like this :
 * Sheet title
 * first row
 * date
 * column header 1: value, column header 2: value, ...
 * second row
 * date
 * column header 1: value, column header 2: value, ...
 * thid row
 * date
 * ...
 * ...
 * @author bostoen.v
 * TODO:
 *	-just use this class with spreadsheet's feeds
 */
public class RssContentParser {
	
	public List<String[]> parse(String text) {

		/*The result is a list of String's array
		 * each row of the list correspond to a row in the rss feed
		 */
	
		final List<String[]> results = new ArrayList<String[]>();

		String[] currentLine = new String[10];
		char[] chaine = new char[text.length()];
		text += '\n';
		text.getChars(0, text.length()-1, chaine, 0);
		String currentWord = "";

		int ligne = 0;
		int colonne = 0;

		//the text to parse is browsed character by character
		for (int i = 0; i < chaine.length; i++) {
			//End of a row
			if (chaine[i] == '\n') {
				ligne++;
				currentLine[colonne] = currentWord.substring(currentWord.indexOf(':') +2);
				results.add(currentLine);
				currentLine =  new String[10];
				currentWord = "";
				colonne = 0;
			} else {
				if (chaine[i] == ',' && chaine[i+1] == ' ') {
					if (currentWord.contains(":")) {
						currentLine[colonne] = currentWord.substring(currentWord.indexOf(':') +2);
					} else {
						currentLine[colonne] = currentWord;
					}
					currentWord = "";
					colonne ++;
				} else {
					currentWord += chaine[i];
				}
			}
		}
		return results;
	}
}
